<?php
	class ConfigControl extends Control{
		function __auto(){
			C('debug',0);
		}
		function base(){
			$tplDirs = dir::tree("./template"); //模板风格
			$tpls = array(); //模板风格信息
			$id = 0;
			foreach ($tplDirs as $tpl) {
            	$xml = $tpl['path'] . '/read.xml';
            	$tpls[$id] = array();
            	if (is_file($xml)) {
                	//获得模板风格的URL地址，用于显示模板图片
                	$tplUrl = __ROOT__.'/template/'.basename($tpl['path']);
                	//获得模板风格的XML定义
                	$tpls[$id] = xml::toArray(file_get_contents($xml));
                	//模板截图
                	$tpls[$id]['short']=$tplUrl .'/'.basename($tpls[$id]['short']);
                	//模板风格名称
                	$tpls[$id]['style']=basename($tpl['path']);
            	}
            	$id++;
        	}//foreach
        	$this->assign("defaultjpg",__ROOT__.'/static/default.png');//分配默认风格图片
        	$this->assign('tpl_styles',$tpls);//分配置所有模板风格数据
        	$db = M("config");				//获得除了模板风格配置的配置项
        	$base_config = $db->where("name<>'style'")->where('ctid=0')->all(); //获得模板配置
        	$tpl_config = $db->where("name='style'")->find();
        	$this->assign("base_config", $base_config);
         	$this->assign("tpl_config", $tpl_config);
			$this->display();
		}
		//修改配置
		function editconfig(){
			$data = $_POST;
        	$db = M('config');
         	foreach($data as $name=>$value){
            	$sql = "UPDATE ".C('DB_PREFIX')."config SET value='$value'
                    WHERE name='$name'";
            	$db->exe($sql);
         	}
         	$allConfig = $db->field("name,value")->all();
         	$dataConfigFile = array();
         	foreach($allConfig  as $conf){
             	$dataConfigFile[$conf['name']]=$conf['value'];
         	}

            $baseconfig=include './config/base.php';
            $allconfig=array_merge($baseconfig,$dataConfigFile);
         	$str = "<?php if(!defined('PATH_HD'))exit;\nreturn ".var_export($allconfig,true).";\n?>";
         	if(@file_put_contents("./config/base.php", $str)){
            	$this->success("修改配置项成功 ");
         	}else{
             $this->error("配置文件不能写，请修改文件权限");
         	}
		}

        //验证码配置显示视图
        public function code(){
            $db=M('config');
            $codedata=$db->where('ctid=2')->all();
            $this->assign('codedata',$codedata);
            $this->display();
        }
        // 修改验证码
        public function changecode(){
            $codedata=$_POST;
            $db=M('config');
            foreach($codedata as $name=>$value){
                $sql = "UPDATE ".C('DB_PREFIX')."config SET value='$value'
                    WHERE name='$name'";
                $db->exe($sql);
            }
            $codeConfig = $db->field("name,value")->where('ctid=2')->all();
            $codeConfigFile = array();
            foreach($codeConfig  as $conf){
                $codeConfigFile[$conf['name']]=$conf['value'];
            }
            $baseconfig=include './config/base.php';
            $allconfig=array_merge($baseconfig,$codeConfigFile);
            $str = "<?php if(!defined('PATH_HD'))exit;\nreturn ".var_export($allconfig,true).";\n?>";
            if(@file_put_contents("./config/base.php", $str)){
                $this->success("修改配置项成功 ");
            }else{
             $this->error("配置文件不能写，请修改文件权限");
            }

        }
        //水印设置显示视图
        public function water(){
            $db=M('config');
            $waterdata=$db->where('ctid=1')->all();
            $this->assign('waterdata',$waterdata);
            $this->display();
        }
        public function changewater(){
            $waterdata=$_POST;
            $db=M('config');
            foreach($waterdata as $name=>$value){
                $sql = "UPDATE ".C('DB_PREFIX')."config SET value='$value'
                    WHERE name='$name'";
                $db->exe($sql);
            }
            $waterConfig = $db->field("name,value")->where('ctid=1')->all();
            $waterConfigFile = array();
            foreach($waterConfig  as $conf){
                $waterConfigFile[$conf['name']]=$conf['value'];
            }
            $baseconfig=include './config/base.php';
            $allconfig=array_merge($baseconfig,$waterConfigFile);
            $str = "<?php if(!defined('PATH_HD'))exit;\nreturn ".var_export($allconfig,true).";\n?>";
            if(@file_put_contents("./config/base.php", $str)){
                $this->success("修改配置项成功 ");
            }else{
             $this->error("配置文件不能写，请修改文件权限");
            }
        }
	}
?>